public class Demo {

    //
    public int tribonacci(int n) {
        if(n == 0) { return 0;}
        if(n == 1 || n == 2) { return 1;}

        int a = 0, b = 1, c = 1, d = 0;
        for(int i = 3; i <= n; i++){
            d = a + b + c;
            a = b;
            b = c;
            c = d;
        }
        return d;
    }

    public int tribonacci2(int n) {
        if(n == 0) { return 0;}
        if(n == 1 || n == 2) { return 1;}

        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = dp[2] = 1;
        for(int i = 3; i <= n; i++){
            dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3];
        }
        return dp[n];
    }

    //
    public int waysToStep(int n) {
        if(n == 1 || n == 2) { return n;}
        if(n == 3) { return 4;}

        int MOD = (int)1e9 + 7;
        int[] dp = new int[n + 1];
        dp[1] = 1; dp[2] = 2; dp[3] = 4;
        for(int i = 4; i <= n; i++){
            dp[i] = ((dp[i - 1] + dp[i - 2]) % MOD + dp[i - 3]) % MOD;
        }
        return dp[n];
    }

    //
    public int minCostClimbingStairs(int[] cost) {
        int n = cost.length;
        int[] dp = new int[n + 1];

        for(int i = 2; i <= n; i++){
            dp[i] = Math.min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);
        }
        return dp[n];
    }

    public int minCostClimbingStairs2(int[] cost) {
        int n = cost.length;

        int prev = 0, cur = 0;
        for(int i = 2; i <= n; i++){
            int next = Math.min(cost[i - 1] + cur, prev + cost[i - 2]);
            prev = cur;
            cur = next;
        }
        return cur;
    }

    //
    public int numDecodings(String s) {
        int n = s.length();
        char[] ch = s.toCharArray();
        int[] dp = new int[n + 1];

        dp[0] = 1;
        if(ch[0] != '0') { dp[1] = 1;}

        for(int i = 2; i <= n; i++){
            if(ch[i - 1] != '0') { dp[i] += dp[i - 1];}
            int t = (ch[i - 2] - '0') * 10 + (ch[i- 1] - '0');
            if(t >= 10 && t <= 26) { dp[i] += dp[i - 2];}
        }
        return dp[n];
    }

    public int numDecodings2(String s) {
        int n = s.length();
        char[] ch = s.toCharArray();
        int[] dp = new int[n];

        if(ch[0] != '0') { dp[0] = 1;}
        if(n == 1) { return dp[0];}

        if(ch[1] != '0' && ch[0] != '0') { dp[1] += 1;}
        int t = (ch[0] - '0') * 10 + ch[1] - '0';
        if(t >= 10 && t <= 26) { dp[1] += 1;}

        for(int i = 2; i < n; i++){
            if(ch[i] != '0') { dp[i] += dp[i - 1];}
            t = (ch[i - 1] - '0') * 10 + ch[i] - '0';
            if(t >= 10 && t <= 26) { dp[i] += dp[i - 2];}
        }
        return dp[n - 1];
    }

    //
    public int uniquePaths(int m, int n) {
        int[][] dp = new int[m + 1][n + 1];
        dp[0][1] = 1;

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
            }
        }
        return dp[m][n];
    }

    public int uniquePaths2(int m, int n) {
        int[][] dp = new int[m][n];
        for(int i = 0; i < m; i++) { dp[i][0] = 1;}
        for(int j = 0; j < n; j++) { dp[0][j] = 1;}

        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = dp[i][j - 1] + dp[i - 1][j];
            }
        }
        return dp[m - 1][n - 1];
    }
}
